package com.rcloud.server.sealtalk.controller;


import com.rcloud.server.sealtalk.constant.ErrorCode;
import com.rcloud.server.sealtalk.controller.param.SubmitStepParam;
import com.rcloud.server.sealtalk.controller.param.UserReportParam;
import com.rcloud.server.sealtalk.domain.UserReport;
import com.rcloud.server.sealtalk.domain.Users;
import com.rcloud.server.sealtalk.exception.ServiceException;
import com.rcloud.server.sealtalk.manager.DocListManager;
import com.rcloud.server.sealtalk.model.response.APIResult;
import com.rcloud.server.sealtalk.model.response.APIResultWrap;
import com.rcloud.server.sealtalk.service.UserReportService;
import com.rcloud.server.sealtalk.service.UsersService;
import com.rcloud.server.sealtalk.util.N3d;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用户举报控制器
 */
@Api(tags = "用户举报相关接口")
@Slf4j
@RestController
@RequestMapping("/user/report")
@AllArgsConstructor
public class UserReportController extends BaseController {

    private UserReportService userReportService;

    private UsersService usersService;


    @ApiOperation("用户举报的接口")
    @PostMapping("/submitReport")
    public APIResult<Object> submitReport(@RequestBody UserReportParam param) throws ServiceException {
        if (StringUtils.isEmpty(param.getContent()) || StringUtils.isEmpty(param.getImageUrl()) || StringUtils.isEmpty(param.getReportUserID())) {
            throw new ServiceException(ErrorCode.PARAM_ERROR);
        }
        Integer userId = getCurrentUserId();
        UserReport paramsReport = UserReport.builder().build();
        paramsReport.setContent(param.getContent());
        paramsReport.setCreateTime(new Date());
        paramsReport.setIsDeal(0);
        paramsReport.setImageUrl(param.getImageUrl());
        paramsReport.setReportUserId(N3d.decode(param.getReportUserID()));
        paramsReport.setUserId(userId);
        userReportService.saveSelective(paramsReport);
        return APIResultWrap.ok("", "举报成功，请耐心等待官方审核！");
    }

    /**
     * 查看用户是否有举报记录
     */
    @GetMapping(value = "/queryReportRecord")
    @ApiOperation(value = "查看用户是否有举报记录", notes = "查看用户是否有举报记录")
    @ApiImplicitParam(name = "mobile", value = "被查询用户手机号", dataType = "String")
    public APIResult<Object> queryReportRecord(@RequestParam String mobile) throws ServiceException {
        Users param = new Users();
        param.setPhone(mobile);
        Users u = usersService.getOne(param);
        if (u == null) {
            throw new ServiceException(ErrorCode.USER_NOT_EXIST);
        }
        Example example = new Example(UserReport.class);
        //注意：排序使用的是列名
        example.setOrderByClause("create_time DESC");
        //如果需要其他条件
        //掌机类型名字
        //条件查询使用的是属性名
        example.createCriteria().andEqualTo("reportUserId", u.getId()).andEqualTo("isDeal", 1);
        List<UserReport> userReportList = userReportService.getByExample(example);
        for (UserReport report : userReportList) {
            Users reportUser = usersService.getByPrimaryKey(report.getReportUserId());
            report.setReportNickname(reportUser.getNickname());
            report.setReportAvatar(reportUser.getPortraitUri());
            Users user = usersService.getByPrimaryKey(report.getUserId());
            report.setUserNickname(user.getNickname());
            report.setUserAvatar(user.getPortraitUri());
        }
        return APIResultWrap.ok(userReportList);
    }
}